package com.neuedu.shixun.hospital2.controller;
import com.neuedu.shixun.hospital2.entity.*;
import com.neuedu.shixun.hospital2.service.*;
import com.neuedu.shixun.hospital2.util.MD5Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin")
@CrossOrigin
public class AdminController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private DoctorService doctorService;
    @Autowired
    private PublicService publicService;
    @Autowired
    private PbamService pbamService;
    @Autowired
    private PbpmSrvice pbpmSrvice;
@PostMapping("regist")
public String regist(@RequestBody RuleForm2 ruleForm2){
    log.info("ruleform2:"+ruleForm2);
    char c=publicService.checkEntity(ruleForm2);
    if (c=='1'){//医生
        if (doctorService.save(ruleForm2).equals("ok")){
            //通过医生idnum获得id
            Doctor doctor =doctorService.getOne(ruleForm2.getIdNum());
            log.info("通过idnum获得"+doctor);
            //注册成功把医生id存入排班表
            Pbam pbam=new Pbam();
            pbam.setDDid(doctor.getDDid());
            pbamService.save(pbam);
            Pbpm pbpm=new Pbpm();
            pbpm.setDDid(doctor.getDDid());
            pbpmSrvice.save(pbpm);
            return "okk";
        }else return "false";
    }else if(c=='0'){//管理员
        if (adminService.save(ruleForm2).equals("ok")){
            return "okk";
        }else return "false";
    }
    else
        return "false";

}
    @PostMapping("login")
    public String login(@RequestBody RuleForm ruleForm){
    log.info("要登录的："+ruleForm);
    char c=publicService.checkEntity(ruleForm);
    if (c=='1'){
        String pass="";
        try {
             pass= MD5Util.EncoderByMd5(ruleForm.getPass());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        log.info("加密后的密码："+pass);
        String str=doctorService.checkOne(ruleForm.getIdNum(),pass,ruleForm.getName());
        if (str.equals("ok"))
            return "doctor";
        else if(str.equals("no"))
            return "no";
        else
            return "false";
    }
    else if (c=='0'){
        String pass="";
        try {
             pass=MD5Util.EncoderByMd5(ruleForm.getPass());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String str=adminService.checkOne(ruleForm.getIdNum(),pass,ruleForm.getName());
        if (str.equals("ok"))
         return "admin";
        else if(str.equals("no"))
            return "no";
        else
            return "false";
    }
    else
        return "false";
    }
    @GetMapping("/getPass")
    public String getPass(){
        return adminService.getPass();
    }
    @PostMapping("/comPass")
    public String comPass(String inputPass){
        log.info("controller"+inputPass);
        return adminService.comPass(inputPass);
    }
    @PostMapping("/comCode")
    public String comCode(@RequestBody Yzm yzm){
    log.info("验证码为："+yzm.getCheckCode()+"输入码为："+yzm.getCheckYzm());
        return adminService.comCode(yzm.getCheckCode(),yzm.getCheckYzm());
    }
    @GetMapping("getAdmin")
    public List<Admin2> getAdmin(){
    List<Admin> list=adminService.getAdmin();
    log.info("lisAdmin"+list);
    List<Admin2> list1=adminService.getAdmin2(list);
    return list1;
    }
    @GetMapping("getOneAdmin")
    public Admin getOneAdmin(String jobNum){
    log.info("jobNum:"+jobNum);
      return adminService.findOneAdmin(jobNum);
    }
    @PostMapping("updateAdmin")
    public Admin updateAdmin(@RequestBody Admin admin){
    log.info("传入的"+admin);
    String pass=admin.getAPassword();
        try {
            admin.setAPassword(MD5Util.EncoderByMd5(pass));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        log.info("传出的"+adminService.updateAdmin(admin));
      return  adminService.updateAdmin(admin);
    }
    @PostMapping("JiaMi")
    public String Jiami(@RequestBody String pass){
        return adminService.JiaMi(pass);
    }
//    @PostMapping("saveAdmin")
//    public Admin saveAdmin(@RequestBody  Admin admin){
//        return adminService.updateAdmin(admin);
//    }
}
